<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/12/28
 * Time: 16:56
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\web\controller;

use app\BaseController;
use app\common\business\Yu;
use app\common\lib\Show;
use app\common\lib\Status;
use think\Exception;
use think\facade\View;

class Index extends WechatBase
{
    public function index()
    {
        $config = $this->wechat_app->jssdk->buildConfig(['updateAppMessageShareData', 'updateTimelineShareData'], false);
//        halt($config);
        View::assign('config', $config);
        $fatherOpenid = $this->request->param('user_openid','','trim');
        View::assign('openid',$fatherOpenid);
        return View::fetch('',[
            "fatherOpenid"=>$fatherOpenid
        ]);
    }

    public function save()
    {
        if (!$this->request->isPost()) {
            return Show::json_show(Status::status_error, "类型错误");
        }
        $name = $this->request->param('name', '', 'trim');
        $mobile = $this->request->param('mobile', '', 'trim');
        $city = $this->request->param('city', '', 'trim');
        $address = $this->request->param('address', '', 'trim');
        $fatherOpenid = $this->request->param('fatherOpenid','','trim');
        $openid = $this->openid;
        $data = [
            "name" => $name,
            "mobile" => $mobile,
            "city" => $city,
            "address" => $address,
            "fatherOpenid"=>$fatherOpenid,
            "openid"=>$openid
        ];
        try {
            $res = (new Yu())->add($data);
        } catch (\Exception $e) {
            throw new Exception($e->getMessage());
        }
        if($res){
            return Show::json_show(Status::status_normal,"ok");
        }else{
            return Show::json_show(Status::status_error,"error");
        }
    }
    public function success(){
        return View::fetch();
    }
    public function error(){
        return View::fetch();
    }
}
